package cn.ncist.controller;

import cn.ncist.common.RespResult;
import cn.ncist.health_interface.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @DubboReference
    private LoginService loginService;

    @PostMapping("/check")
    public RespResult check(HttpServletResponse response, @RequestBody Map map){
        String validateCode = (String) map.get("validateCode");
        String email = (String) map.get("telephone");
        RespResult respResult = loginService.check(email, validateCode);
        if(!respResult.isFlag()){
            //登录失败
            return respResult;
        }
        //登录成功
        //写入Cookie，跟踪用户
        Cookie cookie = new Cookie("login_member_telephone", email);
        cookie.setPath("/");//路径
        cookie.setMaxAge(60*60*24*30);
        response.addCookie(cookie);
        return respResult;
    }
}
